package com.fau.om.action.index;

import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.om.action.PublicAction;
import com.fau.om.entity.MedicalAdmin;
import com.fau.om.entity.MedicalOrders;
import com.fau.om.util.FileNameEncoderUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
/**
 * @desc 订单分配Action
 * @author kangyj
 * @datetime 2014年9月12日,下午8:58:11
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class AllocationAction extends PublicAction implements ModelDriven<MedicalOrders> {

	private static final long serialVersionUID = 2720465214457824659L;
	
	private MedicalOrders orders = new MedicalOrders();
	
	/**
	 * @desc 订单分配tabs页初始化
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月25日,下午2:00:20
	 */
	@Action(value = "allocation_tabs_init", results = {@Result(name = SUCCESS, location = "allocation_tabs_init.jsp")})
	public String tabInit() {
		return SUCCESS;
	}
	
	/**
	 * @desc 订单未分配页面初始化
	 * @return
	 * @author kangyj
	 * @datetime 2014年9月12日,下午9:00:25
	 */
	@Action(value = "allocation_init", results = {@Result(name = SUCCESS, location = "allocation_init.jsp")})
	public String init() {
		return SUCCESS;
	}

	/**
	 * @desc 分页查询订单分配
	 * @return
	 * @author kangyj
	 * @datetime 2014年9月12日,下午9:01:56
	 */
	@Action(value = "allocation_view")
	public String view(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		pageBean = this.baseAllService.getAllocationService().findAllocationByPage(map, orders, admin);
		map.clear();
		map.put("total", pageBean.getTotalRow());
		map.put("data", pageBean.getResult());
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 已分配页面初始化
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月25日,下午4:35:54
	 */
	@Action(value = "allocation_allocated", results = {@Result(name = SUCCESS, location = "allocation_allocated.jsp")})
	public String allocated() {
		return SUCCESS;
	}
	
	/**
	 * @desc 订单部回收已分配订单
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月25日,下午4:52:54
	 */
	@Action(value = "allocation_recover")
	public String recover() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(orders.getId())) {
			boolean flag = this.baseAllService.getAllocationService().validRecover(orders.getId());
			if(flag) {
				this.baseAllService.getAllocationService().recover(orders.getId());
				map.put("success", true);
			} else {
				map.put("success", false);
			}
		}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 中心订单分配tabs页面初始化
	 * @return
	 * @author kangyj
	 * @datetime 2014年9月27日,下午6:28:44
	 */
	@Action(value = "allocation_center_tabs_init", results = {@Result(name = SUCCESS, location = "allocation_center_tabs_init.jsp")})
	public String centerTabsInit() {
		return SUCCESS;
	}
	
	/**
	 * @desc 中心未分配页面初始化
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月25日,下午8:09:10
	 */
	@Action(value = "allocation_center_init", results = {@Result(name = SUCCESS, location = "allocation_center_init.jsp")})
	public String centerInit() {
		return SUCCESS;
	}

	/**
	 * @desc 分页查询中心订单分配
	 * @return
	 * @author kangyj
	 * @datetime 2014年9月27日,下午6:47:18
	 */
	@Action(value = "allocation_center_view")
	public String centerView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		pageBean = this.baseAllService.getAllocationService().findCenterAllocationByPage(map, orders, admin);
		map.clear();
		map.put("total", pageBean.getTotalRow());
		map.put("data", pageBean.getResult());
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 跳转到中心已分配订单页面
	 * @return null
	 * @author kangyj
	 * @datetime 2014年11月25日,下午7:27:17
	 */
	@Action(value = "allocation_center_allocated", results = {@Result(name = SUCCESS, location = "allocation_center_allocated.jsp")})
	public String centerAllocated() {
		return SUCCESS;
	}
	
	/**
	 * @desc 中心订单回收
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月25日,下午7:33:51
	 */
	@Action(value = "allocation_center_recover")
	public String centerRecover() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(orders.getId())) {
			boolean flag = this.baseAllService.getAllocationService().validRecover(orders.getId());
			if(flag) {
				this.baseAllService.getAllocationService().centerRecover(orders.getId());
				map.put("success", true);
			} else {
				map.put("success", false);
			}
		}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 核单组订单分配tabs页面初始化
	 * @return
	 * @author kangyj
	 * @datetime 2014年9月27日,下午6:29:53
	 */
	@Action(value = "allocation_group_tabs_init", results = {@Result(name = SUCCESS, location = "allocation_group_tabs_init.jsp")})
	public String groupTabsInit() {
		return SUCCESS;
	}
	
	/**
	 * @desc 核单组订单未分配页面初始化
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月25日,下午7:37:21
	 */
	@Action(value = "allocation_group_init", results = {@Result(name = SUCCESS, location = "allocation_group_init.jsp")})
	public String groupInit() {
		return SUCCESS;
	}

	/**
	 * @desc 分页查询核单组订单分配
	 * @return
	 * @author kangyj
	 * @datetime 2014年9月27日,下午6:36:40
	 */
	@Action(value = "allocation_group_view")
	public String groupView(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		pageBean = this.baseAllService.getAllocationService().findGroupAllocationByPage(map, orders, admin);
		map.clear();
		map.put("total", pageBean.getTotalRow());
		map.put("data", pageBean.getResult());
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 核单组订单已分配页面初始化
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月25日,下午7:38:00
	 */
	@Action(value = "allocation_group_allocated", results = {@Result(name = SUCCESS, location = "allocation_group_allocated.jsp")})
	public String groupAllocated() {
		return SUCCESS;
	}
	
	/**
	 * @desc 核单组订单回收
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月25日,下午7:41:37
	 */
	@Action(value = "allocation_group_recover")
	public String groupRecover() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(orders.getId())) {
			boolean flag = this.baseAllService.getAllocationService().validRecover(orders.getId());
			if(flag) {
				this.baseAllService.getAllocationService().groupRecover(orders.getId());
				map.put("success", true);
			} else {
				map.put("success", false);
			}
		}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}

	/**
	 * @desc 订单分配到中心
	 * @return null
	 * @author kangyj
	 * @datetime 2014年9月15日,下午5:10:47
	 */
	@Action(value = "allocation_allocate_center")
	public String allocateCenter(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(orders.getId()) && StringUtils.isNotBlank(orders.getCenterId())) {
			this.baseAllService.getAllocationService().allocateCenter(orders.getId(), orders.getCenterId());
			map.put("success", true);
		}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 订单分配到组
	 * @return null
	 * @author kangyj
	 * @datetime 2014年9月18日,上午10:31:22
	 */
	@Action(value = "allocation_allocate_group")
	public String allocate_group(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(orders.getId()) && StringUtils.isNotBlank(orders.getGroupId())) {
			this.baseAllService.getAllocationService().allocateGroup(orders.getId(), orders.getGroupId());
			map.put("success", true);
		}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 订单分配到员工
	 * @return
	 * @author kangyj
	 * @datetime 2014年9月18日,上午10:35:18
	 */
	@Action(value = "allocation_allocate_user")
	public String allocateUser(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(orders.getCheckUserId())) {
			this.baseAllService.getAllocationService().allocateUser(orders.getId(), orders.getCheckUserId());
			map.put("success", true);
		} else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 查询用户中心集合
	 * @return
	 * @author kangyj
	 * @datetime 2014年9月27日,下午3:16:44
	 */
	@Action(value = "allocation_center_list")
	public String findUserCenterList(){
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		list = this.baseAllService.getAllocationService().findUserCenterList(admin.getEnterpriseId());
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 根据中心查询核单组集合
	 * @return
	 * @author kangyj
	 * @datetime 2014年9月27日,下午3:18:10
	 */
	@Action(value = "allocation_group_list")
	public String findUserGroupList(){
		HttpServletResponse response = ServletActionContext.getResponse();
		list = this.baseAllService.getAllocationService().findUserGroupList(orders.getCenterId());
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 根据核单组查询用户集合
	 * @return
	 * @author kangyj
	 * @datetime 2014年9月27日,下午9:20:20
	 */
	@Action(value = "allocation_user_list")
	public String findUserList(){
		HttpServletResponse response = ServletActionContext.getResponse();
		list = this.baseAllService.getAllocationService().findUserList(orders.getGroupId());
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 查询当前用户所在中心，核单组的集合
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月25日,下午8:38:38
	 */
	@Action(value = "allocation_cur_cen_group")
	public String findCurCenGroupList() {
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		list = this.baseAllService.getAllocationService().findCurCenGroupList(admin);
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 查询当前用户所在核单组，核单员集合
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月25日,下午9:14:56
	 */
	@Action(value = "allocation_cur_gro_user")
	public String findCurGroUserList() {
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		list = this.baseAllService.getAllocationService().findCurGroUserList(admin);
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 订单分配-导出当前页未分配订单
	 * @return null
	 * @author dangjh
	 * @datetime 2014年10月20日,上午11:39:19
	 */
	@Action(value = "allocation_init_export_current")
	public String currentExport() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		String ids = request.getParameter("ids");
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"未分配订单表.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getAllocationService().exportOrdersCurrent(ids);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @desc 核单部订单分配-导出所有未分配订单
	 * @return null
	 * @author suny
	 * @datetime 2014年10月21日,下午11:51:37
	 */
	@Action(value = "allocation_init_export_all")
	public String allExport() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"核单部订单未分配订单表.xls"));
			response.setContentType("application/vnd.ms-excel");
			MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getAllocationService().exportOrdersAll(orders, admin);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 中心订单分配-导出所有未分配订单
	 * @return null
	 * @author suny
	 * @datetime 2014年10月21日,下午11:51:37
	 */
	@Action(value = "allocation_center_export_all")
	public String allCenterExport() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"中心订单未分配订单表.xls"));
			response.setContentType("application/vnd.ms-excel");
			MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getAllocationService().exportCenterOrdersAll(orders, admin);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 核单组订单分配-导出所有未分配订单
	 * @return null
	 * @author suny
	 * @datetime 2014年10月21日,下午11:51:37
	 */
	@Action(value = "allocation_group_export_all")
	public String allGroupExport() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"核单组订单未分配订单表.xls"));
			response.setContentType("application/vnd.ms-excel");
			MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getAllocationService().exportGroupOrdersAll(orders, admin);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 核单部订单分配-导出当前页已分配订单
	 * @return null
	 * @author dangjh
	 * @datetime 2014年10月20日,上午11:39:19
	 */
	@Action(value = "allocated_init_export_current")
	public String currentAllocatedExport() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		String ids = request.getParameter("ids");
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"已分配订单表.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getAllocationService().exportAllocatedOrdersCurrent(ids);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 核单部订单分配-导出所有已分配订单
	 * @return
	 * @author suny
	 * @datetime 2014年10月21日,下午11:51:37
	 */
	@Action(value = "allocated_init_export_all")
	public String allAllocatedExport() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"核单部已分配订单表.xls"));
			response.setContentType("application/vnd.ms-excel");
			MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getAllocationService().exportAllocatedOrdersAll(orders, admin);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 中心订单分配-导出所有已分配订单
	 * @return
	 * @author suny
	 * @datetime 2014年10月21日,下午11:51:37
	 */
	@Action(value = "allocated_center_export_all")
	public String allAllocatedCenterExport() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"中心已分配订单表.xls"));
			response.setContentType("application/vnd.ms-excel");
			MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getAllocationService().exportAllocatedCenterOrdersAll(orders, admin);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 核单组订单分配-导出所有已分配订单
	 * @return
	 * @author suny
	 * @datetime 2014年10月21日,下午11:51:37
	 */
	@Action(value = "allocated_group_export_all")
	public String allAllocatedGroupExport() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"核单组已分配订单表.xls"));
			response.setContentType("application/vnd.ms-excel");
			MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getAllocationService().exportAllocatedGroupOrdersAll(orders, admin);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public MedicalOrders getModel() {
		return orders;
	}

}
